package com.sgb.kjwl.view.ui.fragment.material;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.EditorInfo;

import androidx.annotation.Nullable;
import androidx.databinding.DataBindingUtil;
import androidx.fragment.app.Fragment;

import com.sgb.kjwl.AppFragment;
import com.sgb.kjwl.R;
import com.sgb.kjwl.databinding.FragmentAllGoodsList3Binding;
import com.sgb.kjwl.di.material.DaggerMaterialFragComponent;
import com.sgb.kjwl.di.material.MaterialFragModule;
import com.sgb.kjwl.model.entity.supplier3.DictEntity3;
import com.sgb.kjwl.view.event.CategoryEvent;
import com.sgb.kjwl.view.ui.activity.home.HomeCategoryActivity;
import com.sgb.kjwl.view.ui.activity.self.publish.GoodsPublishActivity;
import com.sgb.kjwl.view.ui.adapter.CommPagerAdapter3;
import com.sgb.kjwl.viewmodel.material.AllGoodsMaterialListModel3;
import com.sgb.link.event.MaterialEvent;
import com.swgk.core.base.di.AppComponent;
import com.swgk.core.event.EventConstant;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;

public class AllMaterialListFragment3 extends AppFragment {

    @Inject
    AllGoodsMaterialListModel3 viewModel;
    private FragmentAllGoodsList3Binding binding;
    private String title;
    private String id;
    private String fId;  //用户筛选类别查询
    private boolean isCreate = false;
    private boolean init = true;
    private boolean isVisible;
    private ArrayList<Fragment> mFragments;
    private int type;
    private String threeLevelClassId = "";
    private String nameId = "";
    private int current = 0;
    private String threeName;

    public static AllMaterialListFragment3 start(String title, String id, String mId, int type) {
        AllMaterialListFragment3 fragment = new AllMaterialListFragment3();
        Bundle bundle = new Bundle();
        bundle.putString("title", title);
        bundle.putString("id", id);
        bundle.putString("mId", mId);
        bundle.putInt("type", type);
        fragment.setArguments(bundle);
        return fragment;
    }

    public static AllMaterialListFragment3 start(String title, String id, String mId, int type, String threeLevelClassId, String nameId) {
        AllMaterialListFragment3 fragment = new AllMaterialListFragment3();
        Bundle bundle = new Bundle();
        bundle.putString("title", title);
        bundle.putString("id", id);
        bundle.putString("mId", mId);
        bundle.putString("nameId", nameId);
        bundle.putInt("type", type);
        bundle.putString("threeLevelClassId", threeLevelClassId);
        fragment.setArguments(bundle);
        return fragment;
    }

    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
        isVisible = isVisibleToUser;
        if (!isCreate) {
            return;
        }
        if (isVisibleToUser) {
            if (init) {
                init = false;
                initView();
                init(fId, false);
            }
        }
    }

    @Override
    protected void setupFragmentComponent(AppComponent appComponent) {
        DaggerMaterialFragComponent.builder()
                .appComponent(appComponent)
                .materialFragModule(new MaterialFragModule(this))
                .build()
                .inject(this);
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        isCreate = true;
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        binding = DataBindingUtil.inflate(inflater, R.layout.fragment_all_goods_list3, container, false);
        return binding.getRoot();
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        title = getArguments().getString("title");
        id = getArguments().getString("id");
        fId = getArguments().getString("mId");
        nameId = getArguments().getString("nameId");
        type = getArguments().getInt("type", 0);
        threeLevelClassId = getArguments().getString("threeLevelClassId");
        if (init && isVisible) {
            init = false;
            initView();
            init(fId, false);
        }
    }

    protected void initView() {
        binding.ivBack.setVisibility(View.VISIBLE);
        binding.ivBack.setOnClickListener(this);
        binding.rightView.setOnClickListener(this);
        binding.publishView.setOnClickListener(this);
        binding.locView.setOnClickListener(this);
        binding.tvTitle.setText(title);

        binding.editSearch.setImeOptions(EditorInfo.IME_ACTION_SEARCH);
        binding.editSearch.setOnKeyListener((v, keyCode, event) -> {
            if (keyCode == KeyEvent.KEYCODE_ENTER && event.getAction() == KeyEvent.ACTION_DOWN) {
                closeKeybord();
                EventBus.getDefault().postSticky(MaterialEvent.make().setMessageTag(EventConstant.EVENT_MATERIAL_FILTER).setMessageData(binding.editSearch.getText().toString()));
            }
            return false;
        });
    }

    private void init(String fId, boolean isRefresh) {
        viewModel.getFilterTypeModel(fId, isRefresh);
    }

    public void showTypeView(List<DictEntity3> datas, boolean isRefresh) {
        if (datas == null || datas.size() == 0) {
            binding.viewpager.setVisibility(View.GONE);
            binding.emptyView.setVisibility(View.VISIBLE);
            return;
        }
        if (isRefresh) {
            for (int i = 0; i < datas.size(); i++) {
                if (threeName.equals(datas.get(i).getName())) {
                    current = i;
                }
            }
            binding.viewpager.setCurrentItem(current);
            MaterialListFragment4 fragment4 = (MaterialListFragment4) mFragments.get(current);
            fragment4.setCurrentItem(id, fId, nameId, threeLevelClassId, current);
        } else {
            mFragments = new ArrayList<>();
            int size = datas.size();
            String[] titles = new String[size];
            for (int i = 0; i < size; i++) {
                mFragments.add(MaterialListFragment4.newInstance(id, fId, datas.get(i).getId(), nameId, i, type));
                titles[i] = datas.get(i).getName();
                if (!TextUtils.isEmpty(threeLevelClassId) && threeLevelClassId.equals(datas.get(i).getId())) {
                    current = i;
                }
            }
            CommPagerAdapter3 mAdapter = new CommPagerAdapter3(getChildFragmentManager(), mFragments, titles);
            binding.viewpager.setAdapter(mAdapter);
            binding.tablayout.setupWithViewPager(binding.viewpager);
            binding.viewpager.setCurrentItem(current);
        }

    }

    public void changeType(int position) {
        binding.viewpager.setCurrentItem(position);
    }

    public void setCurrentItem(String id, String fId, String nameId, String threeLevelClassId,String threeName) {
        this.id = id;
        this.fId = fId;
        this.nameId = nameId;
        this.threeLevelClassId = threeLevelClassId;
        this.threeName = threeName;
        init(fId, true);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.right_view:
                EventBus.getDefault().postSticky(CategoryEvent.make(EventConstant.EVENT_CATEGORY_CHOOSE, 0));
                HomeCategoryActivity.start(getActivity());
                break;
            case R.id.publish_view:
                GoodsPublishActivity.start(getActivity());
                break;
        }
    }
}
